package org.ljy;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Class;
import java.net.Inet4Address;
import java.net.Socket;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
    public static void main(String[] args) throws IOException {
        // 对 List<QueueEntry> 按故障序列长度升序排序
        /*Map<Integer,String> hello = new HashMap<>();

        for(int i = 0;i<10;i++){
            hello.put(i,"hello"+i);
        }

        System.out.println(hello);

        String find = hello.computeIfPresent(10,(k,v)->v);
        System.out.println(find);

        DecimalFormat df = new DecimalFormat("0.00%");
        String a = df.format(0.111);
        System.out.println(a);*/
/*        ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();

        for(int i = 0;i<20;i++){
            String nodeIP = "172.30.0."+(i%5+2);
            System.out.println(nodeIP);
            AtomicInteger appearIdx = map.computeIfAbsent(nodeIP, k->new AtomicInteger(0));
            int k = appearIdx.incrementAndGet();
            System.out.println(k);
        }*/

        Random r = new Random();
        Map<Integer,Double> iopRankCrash = new HashMap<>();

        // 写入键值对
        iopRankCrash.put(9, 5.844414810524482);
        iopRankCrash.put(1, 5.124950422630503);
        iopRankCrash.put(7, 4.633315466155872);
        iopRankCrash.put(4, 4.564898084465733);
        iopRankCrash.put(5, 4.396469250752748);
        iopRankCrash.put(2, 3.8755275685352215);
        iopRankCrash.put(3, 3.505424410123102);
        iopRankCrash.put(8, 2.706881408596199);
        iopRankCrash.put(0, 2.1246814929965008);
        iopRankCrash.put(6, 1.090429100031074);

        List<Integer> iopRankCr = iopRankCrash.entrySet().stream()
                .sorted(Map.Entry.<Integer,Double>comparingByValue().reversed())
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

        // 原方式：先转LinkedHashMap，再提取key到List
        Map<Integer,Double> sortedIOPCrash = iopRankCrash.entrySet().stream()
                .sorted(Map.Entry.<Integer,Double>comparingByValue().reversed())
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (oldVal,newVal)->oldVal,
                        LinkedHashMap::new
                ));
        //sortedIOPCrash.forEach((k,v)-> iopRankCr.add(k));

        System.out.println(sortedIOPCrash);

        System.out.println(iopRankCr);

        boolean isGoodCrash = false;


    }

    public enum FaultPos{
        BEFORE,AFTER;
    }




}